{% extends "base.html" %}

{% block title %}Changement de mot de passe{% endblock %}
{% block content_title %}Changement de mot de passe{% endblock %}
{% block image %}<img src="/media/images/oxygen/lock.png" />{% endblock %}

{% block content %}
<script language="javascript">
function passwordChanged() {
    var strength = dojo.byId('strength');
    var strongRegex = new RegExp("^(?=.{9,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\W).*$", "g");
    var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    var enoughRegex = new RegExp("(?=.{6,}).*", "g");
    var pwd = dojo.byId("id_new_password1");
    var button = dojo.byId("button_submit");
    var ok = false;
    
    if (pwd.value.length==0) {
        strength.innerHTML = '';
    } else if (false == enoughRegex.test(pwd.value)) {
        strength.innerHTML = '<span style="color:red;">Nombre de carractères insuffisant</span>';
    } else if (strongRegex.test(pwd.value)) {
        strength.innerHTML = '<span style="color:green;">Excellent!</span>';
        ok = true
    } else if (mediumRegex.test(pwd.value)) {
        strength.innerHTML = '<span style="color:orange;">Bonne</span>';
        ok = true;
    } else { 
        strength.innerHTML = '<span style="color:red;">Faible</span>';
        ok = true;
    }
    
    if (! ok) {
        dojo.style(button, "display", "none");
    } else {
        dojo.style(button, "display", "inline");
    }
}

function gen_numb(min, max){  
    return (Math.floor(Math.random() * (max - min)) + min);  
}  

function gen_chr(num, lwr, upr, oth, ext){  
    var num_chr = "0123456789";  
    var lwr_chr = "abcdefghijklmnopqrstuvwxyz";  
    var upr_chr = lwr_chr.toUpperCase();  
    var oth_chr = "`~!@#$%^&*()-_=+[{]}\\|;:'\",<.>/? ";  
    var sel_chr = ext;  

    if(num == true)  
        sel_chr += num_chr;  
    if(lwr == true)  
        sel_chr += lwr_chr;  
    if(upr == true)  
        sel_chr += upr_chr;  
    if(oth == true)  
        sel_chr += oth_chr;  
    return sel_chr.charAt(gen_numb(0, sel_chr.length));  
}  

function gen_pass(len, ext, bgn_num, bgn_lwr, bgn_upr, bgn_oth,  
    flw_num, flw_lwr, flw_upr, flw_oth){  
    var res = "";  

    if(len > 0){  
        res += gen_chr(bgn_num, bgn_lwr, bgn_upr, bgn_oth, ext);  
        for(var i=1;i<len;i++)  
            res += gen_chr(flw_num, flw_lwr, flw_upr, flw_oth, ext);  
        return res;  
    }  
}  

function get_random_password(){
    return gen_pass(9, '123451234', true, true, true, false, true, true, true, false);
}
// End -->
</script>

<fieldset class="block">
    <legend>Changement de mot de passe</legend>
    <p>Nous vous recommandons d'utiliser le générateur de mot de passe,
    afin de garantir la sécurité de votre compte utilisateur, et de vos données privées.<br />
    </p>
    <form action="" method="post">{% csrf_token %}
    {% if form.errors %}
      <p style="color: red;">Erreurs.</p>
    {% endif %}
    
    <table class="search">
        <tr>
            <td>{{ form.old_password.label_tag }}{{ form.old_password.errors }}</td>
            <td>{{ form.old_password }}</td>
            <td style="font-weight: normal;">Qualité du mot de passe</td>
        </tr>
        <tr>
            <td>{{ form.new_password1.label_tag }}{{ form.new_password1.errors }}</td>
            <td><input type="password" name="new_password1" id="id_new_password1"/></td>
            <td><span id="strength"></span></td>
        </tr>
        <tr>
            <td>{{ form.new_password2.label_tag }}{{ form.new_password2.errors }}</td>
            <td><input type="password" name="new_password2" id="id_new_password2"/></td>
            <td id="generated_password">&nbsp;</td>
        </tr>
    </table>

    <span id="button_generate" dojoType="dijit.form.Button" type="button">Générer un mot de passe</span>
    <span id="button_submit" class="bouton"><img class="bouton_img" src="/media/images/oxygen/ok.png" />
      <input type="submit" value="Modifier le mot de passe" />
    </span>
    </form>
    
    <script type="text/javascript">
    dojo.addOnLoad(function(){
        dojo.byId("id_old_password").focus();
        dojo.connect(dojo.byId("new_password1"), "onkeyup", passwordChanged);
        dojo.connect(dojo.byId("button_generate"), "onclick", function() {
            var pass = get_random_password();
            dojo.byId("id_new_password1").value = pass;
            dojo.byId("id_new_password2").value = pass;
            dojo.byId("generated_password").innerHTML = pass;
            passwordChanged();
        });
    });
    </script>
</fieldset>
{% endblock %}
